
function navActive(index){
    $('.layui-nav').children('li').eq(index).addClass('layui-this')
}

function portalNavActive(index){
    $('.user-result-wrap li').find('a').eq(index).addClass('active');
}

$(function(){

    $('.scroll-wrap').height(window.innerHeight-70);

    $('.home-bot-wrap .live-list-wrap li a').hover(function(){
        $(this).parent('li').addClass('active').siblings('li').removeClass('active');
    },function(){
        $(this).parent('li').removeClass('active');
    });

    //输入密码观看直播或回放
    $('.live-list-wrap li a').off('click').on('click',function(){
        if($(this).parents('ul').hasClass('advance-list-wrap')){
            return false;
        }
        var pw = $(this).parent('li').attr('data-pw'),
            llid = $(this).parent('li').attr('data-llid'),
            live_href = $(this).parent('li').attr('data-href');
        var data_ex,url='';
        if($(this).parent('li').attr('data-url')){
            url = $(this).parent('li').attr('data-url');
            data_ex = {mid:llid};
        }else {
            data_ex = {llid:llid};
            url = 'Course/password';
        }
        if(pw=='0'){
            window.location.href = live_href;
        }else {
            layer.open({
                type: 1,
                title: false,
                resize: false,
                shadeClose:false,
                close:false,
                content: '<form id="live-pw-wrap" class="form-wrap" action="">'+
                '<h1>视频密码</h1>'+
                '<div class="item">'+
                '<!--[if IE]>'+
                '<span class="placeholder">请输入视频验证码</span>'+
                '<![endif]-->'+
                '<input name="pw-input" placeholder="请输入视频密码" type="password" autocomplete="new-password" />'+
                '</div>'+
                '<div class="item-wrap" style="text-align: center;">'+
                '<div style="display: inline-block">'+
                '<span class="layui-btn layui-btn-normal btn-modify" style="margin-right: 10px;">确定</span>'+
                '<span class="layui-btn layui-btn-primary" onclick="layer.closeAll()">取消</span>'+
                '</div>'+
                '</div>'+
                '</form>',
                success: function(layero, index){
                    iService.placehoder($('#live-pw-wrap input'));
                    var pw_input = $('input[name=pw-input]');
                    var btn_modify_el = $('#live-pw-wrap .btn-modify');

                    var pwValy = function(){
                        var param = {
                            url:url,
                            data:{
                                password: $('input[name=pw-input]').val()
                            }
                        };
                        $.extend(param.data,data_ex);
                        function successFun(data){
                            console.log(data);
                            if(data.code==200){
                                window.location.href = live_href;
                            }else {
                                layer.msg(data.data.message, {icon: 2,anim: 6,time:2*1000});
                            }
                        }
                        $.JsonRpc(param,successFun);
                    };
                    pw_input.on('keydown',function(event){
                        if (event.keyCode == 13)
                        {
                            pwValy();
                        }
                    });
                    btn_modify_el.on('click',function(){
                        pwValy();
                    });
                    pw_input = null;
                    btn_modify_el = null;
                }
            });
        }
    });

    //确定取消预订
    $('.advance-list-wrap .pic-wrap').off('click').on('click','.btn-sure,.btn-cancel',function(){
        if($(this).hasClass('btn-sure')){
            $(this).addClass('btn-cancel').removeClass('btn-sure')
        }else {
            $(this).addClass('btn-sure').removeClass('btn-cancel')
        }
        var llid = $(this).attr('data-llid');
        var li_this = $(this).parents('li');
        var param = {
            url:'Course/enroll',
            data:{
                llid:llid
            }
        };
        function successFun(data){
            if(data.code==200){
                li_this.find('.focus-num').html(data.data.number)
            }
        }
        $.JsonRpc(param,successFun);
    });

    $(".good-list-wrap li").hover(function () {
        $(this).addClass('active').siblings('li').removeClass('active');
    }, function () {
        $(this).removeClass('active');

    });

    layui.use(['element','layer','form', 'laypage', 'laydate'], function(){
        var layer = layui.layer //获得layer模块
            ,laypage = layui.laypage //获得laypage模块
            ,laydate = layui.laydate //获得laydate模块
            ,form = layui.form //获得laydate模块
            ,element = layui.element;

        $("#login-btn").off('click').on('click',function(){
            layer.open({
                type: 1,
                title: false,
                resize: false,
                shadeClose:false,
                content: '<form id="login-wrap" action="">'+
                '<h1>登录</h1>'+
                '<div class="item">'+
                '<span>输入手机号</span>'+
                '<input type="text">'+
                '</div>'+
                '<div class="item">'+
                '<span>输入验证码</span>'+
                '<input type="text">'+
                '</div>'+
                '<a class="btn-getCode fr" href="javascript:void(0)">点击获取验证码</a>'+
                '<a class="btn-getCode disable" href="javascript:void(0)">60s后重新获取</a>'+
                '<div id="sub-login">登录</div>'+
                '</form>',
                success: function(layero, index){
                    iService.placehoder($('#login-wrap input'));
                }
            });
        });
        iService.placehoder($('input[type=text]'));

        //所有分页
        var count = $('#live-page-wrap').attr('data-page-total');
        var limit = $('#live-page-wrap').attr('data-page-per');
        laypage.render({
            elem: 'live-page-wrap'
            ,count: count   //数据总数，从服务端得到
            ,pages:2
            ,limit: limit
            ,groups: 6
            ,theme :'#1dddac'
            ,layout: ['prev','page','next','skip']
            ,curr:function(){
                var page = location.search.match(/page=(\d+)/);
                return page ? page[1] : 1;
            }()
            ,jump: function(obj, first){
                if(!first){
                    if(location.search.match(/gid=(\d+)/)){
                        location.href = location.search.split('&')[0]+'&page='+obj.curr;
                    }else {
                        location.href = '?page='+obj.curr;
                    }
                }
            }
        });

        $('.pt-wrap .avatar-wrap').hover(function(){
            $('.opacity-wrap').stop().fadeIn();
        },function(){
            $('.opacity-wrap').stop().fadeOut();
        });

        //修改个人资料
        function modifyInfo(){
            var param = {
                url:'user/sitePost',
                data:{
                    nickname:$('input[name=nickname]').val(),
                    section:$('input[name=section]').val(),
                    duties:$('input[name=duties]').val(),
                    sex:$('input[name=sex]:checked').val()
                }
            };
            function successFun(data){
                console.log(data);
                if(data.code!=200){
                    layer.msg(data.data.message, {icon: 2,anim: 6,time:2*1000});
                }else {
                    layer.closeAll();
                    layer.msg('个人信息修改成功',{anim:0,time:1500});
                    data = data.data;
                    //返回内容如下
                    $('b.nickname').html(data.data.nickname);
                    $('b.section').html(data.data.section);
                    $('b.duties').html(data.data.duties);

                    $('b.nickname').attr('data-nickname',data.data.nickname);
                    $('b.section').attr('data-section',data.data.section);
                    $('b.duties').attr('data-duties',data.data.duties);
                    var sex_text = '暂无';
                    if(data.data.sex){
                        data.data.sex==1?sex_text='男':(data.data.sex==2?sex_text='女':sex_text='保密');
                    }
                    $('b.sex').attr('data-sex',data.data.sex).html(sex_text);
                }
            }
            $.JsonRpc(param,successFun);
        }

        $('.pt-info-edit').off('click').on('click',function(){

            var sex_input =
                            '<input type="radio" name="sex" value="1" title="男">'+
                            '<input type="radio" name="sex" value="2" title="女">'+
                            '<input type="radio" name="sex" value="3" title="保密">';

            var sex_val = $('.sex').attr('data-sex');

            if(sex_val==1){
                sex_input =
                    '<input type="radio" name="sex" value="1" title="男" checked>'+
                    '<input type="radio" name="sex" value="2" title="女" >'+
                    '<input type="radio" name="sex" value="3" title="保密">';
            }else if(sex_val==2){
                sex_input =
                    '<input type="radio" name="sex" value="1" title="男">'+
                    '<input type="radio" name="sex" value="2" title="女" checked>'+
                    '<input type="radio" name="sex" value="3" title="保密">';
            }else if(sex_val==3){
                sex_input =
                    '<input type="radio" name="sex" value="1" title="男">'+
                    '<input type="radio" name="sex" value="2" title="女" >'+
                    '<input type="radio" name="sex" value="3" title="保密" checked>';
            }

            layer.open({
                type: 1,
                title: false,
                resize: false,
                shadeClose:false,
                content: '<form id="portal-info-wrap" class="form-wrap layui-form" action="">'+
                '<h1>个人设置</h1>'+
                '<div class="item-wrap">'+
                '<b>姓名：</b>'+
                '<div class="item">'+
                '<span class="placeholder">输入姓名</span>'+
                '<input name="nickname" value="'+$('.nickname').attr('data-nickname')+'" type="text">'+
                '</div>'+
                '</div>'+
                '<div class="item-wrap">'+
                '<b>部门：</b>'+
                '<div class="item">'+
                '<span class="placeholder">输入部门</span>'+
                '<input name="section" value="'+$('.section').attr('data-section')+'" type="text">'+
                '</div>'+
                '</div>'+
                '<div class="item-wrap">'+
                '<b>职务：</b>'+
                '<div class="item">'+
                '<span class="placeholder">输入职务</span>'+
                '<input name="duties" value="'+$('.duties').attr('data-duties')+'" type="text">'+
                '</div>'+
                '</div>'+
                '<div class="item-wrap">'+
                '<b>性别：</b>'+
                '<div class="item layui-input-block" style="border: none;">'+
                sex_input+
                '</div>'+
                '</div>'+
                '<div class="item-wrap" style="text-align: center;">'+
                '<div style="display: inline-block">'+
                '<span class="layui-btn layui-btn-normal btn-modify" style="margin-right: 10px;">保存</span>'+
                '<span class="layui-btn layui-btn-primary" onclick="layer.closeAll()">取消</span>'+
                '</div>'+
                '</div>'+
                '</form>',
                success: function(layero, index){
                    iService.placehoder($('.form-wrap input'));
                    form.render(); //更新全部
                    form.render('select'); //刷新select选择框渲染
                    $('.btn-modify').off('click').on('click',function(){
                        modifyInfo();
                    })
                }
            });
        });

        //修改头像
        $('.user-result-wrap .avatar-wrap').off('click').on('click',function(){

            var img_src = $('.user-result-wrap .avatar-wrap .avatar').attr('src');
            layer.open({
                type: 1,
                title: false,
                resize: false,
                shadeClose:false,
                content: '<form id="user-pic-form" class="form-wrap layui-form" action="">'+
                '<h1>修改头像</h1>'+
                '<img class="avatar" src="'+img_src+'"/><br />'+
                '<a href="javascript:void(0);" id="upload_btn_wrap">上传图片<input id="upload_btn" type="file" accept="image/jpg,image/jpeg,image/png" name=img></a>'+
                '<div class="item-wrap" style="text-align: center;">'+
                '<div style="display: inline-block">'+
                '<span class="layui-btn layui-btn-normal btn-save" style="margin-right: 10px;">保存</span>'+
                '<span class="layui-btn layui-btn-primary" onclick="layer.closeAll()">取消</span>'+
                '</div>'+
                '</div>'+
                '</form>',
                success: function(layero, index){
                    form.render(); //更新全部
                    form.render('select'); //刷新select选择框渲染

                    $('#upload_btn').off('change').on('change',function(){
                        var file_form = new FormData($('#user-pic-form')[0]); //提交表单所有数据
                        $.ajax({
                            type: "post",
                            url: "/about/upload",
                            data: file_form,
                            processData: false,
                            contentType: false,
                            async: true,
                            datatype: "JSON",
                            success: function (obj) {
                                var data = $.parseJSON(obj);
                                console.log(data);
                                if (data.code != 200) {
                                    layer.msg(data.data.message, {icon: 2,anim: 6,time:2*1000});
                                } else {
                                    $('#user-pic-form .avatar').attr({'src':url_pic+data.data.path+data.data.file,'data-path':data.data.path,'data-file':data.data.file})
                                }
                                $('#user-pic-form')[0].reset();
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.log(XMLHttpRequest.readyState);
                            }
                        });
                    });

                    $('#user-pic-form .btn-save').on('click',function(){
                        var param = {
                            url:'user/sitePost',
                            data:{
                                path:$('#user-pic-form .avatar').attr('data-path'),
                                file:$('#user-pic-form .avatar').attr('data-file')
                            }
                        };
                        function successFun(data){
                            console.log(data);
                            if(data.code!=200){
                                data = data.data;
                                layer.msg(data.message, {icon: 2,anim: 6,time:2*1000});
                            }else {
                                data = data.data;
                                layer.closeAll();
                                layer.msg(data.message,{anim:0,time:1500});
                                $('.user-result-wrap .avatar-wrap .avatar').attr('src',data.data.avatar);
                            }
                        }
                        $.JsonRpc(param,successFun);

                    });
                }
            });
        });

        //提交评论
        var btn_com_send = $('.fd-wrap #btn-send');
        var el_input_com = $('.forum-detail-wrap .comment-input');
        function commontCommit(){
            var sid = btn_com_send.attr('data-sid'),
                avatar_src = btn_com_send.attr('data-avatar'),
                nickname = btn_com_send.attr('data-nickname'),
                content = btn_com_send.siblings('.comment-input').val();
            btn_com_send.siblings('.comment-input').val('');
            var param = {
                url:'society/commentPost',
                data:{
                    sid :sid,
                    content :content
                }
            };
            function successFun(data){
                if(data.code!=200){
                    layer.msg(data.data.message, {icon: 2,anim: 6,time:2*1000});
                }else {
                    $('b.comment-num').html(parseInt($('b.comment-num').html())+1);  //后台返回的条数或者 +1
                    $(
                        '<li>'+
                        '<div class="comment-info clearfix">'+
                        '<img class="avatar" src="'+avatar_src+'" alt="">&nbsp;'+
                        '<span>'+nickname+'</span>'+
                        '<i class="fr">'+iService.CurentTime()+'</i>'+
                        '</div>'+
                        '<p class="comment-font">'+content+'</p>'+
                        '</li>'
                    ).prependTo('.comment-list-wrap');
                    layer.msg('评论成功',{anim:0,time:1500});
                    //返回内容如下
                }
            }
            $.JsonRpc(param,successFun);
        }
        btn_com_send.off('click').on('click',function(){
            commontCommit();
        });
        el_input_com.off('keydown').on('keydown',function(event){
            if (event.keyCode == 13)
            {
                commontCommit();
            }
        });

        //子评论
        laypage.render({
            elem: 'comments-page-wrap'
            ,count: $('b.comment-num').eq(1).html()   //数据总数，从服务端得到
            ,pages:2
            ,limit: 24  //每页的个数
            ,groups: 6
            ,theme :'#1dddac'
            ,layout: ['prev','page','next','skip']
            ,curr:function(){
                var page = location.search.match(/page=(\d+)/);
                return page ? page[1] : 1;
            }()
            ,jump: function(obj, first){
                if(!first){
                    var param = {
                        url:'society/comment',
                        data:{
                            sid : iService.getQueryString('sid'),
                            page: obj.curr
                        }
                    };
                    $('.comment-list-wrap').html('<img style="display: block;margin: 40px auto;" src="/index/js/layui-master/src/css/modules/layer/default/loading-1.gif"/>');
                    $.JsonRpc(param,successFun);
                    function successFun(data){
                        console.log(data);
                        if(data.code!=200){
                            layer.msg(data.data.message, {icon: 1,anim: 6,time:2*1000});
                            $('.comment-list-wrap').html('');
                        }else {
                            var data_lsit = data.data.lists,data_html='';
                            $.each(data_lsit,function(i){
                                data_html += '<li>'+
                                             '<div class="comment-info clearfix">'+
                                             '<img class="avatar" src="'+data_lsit[i].avatar+'" alt="">&nbsp;'+
                                             '<span>'+data_lsit[i].nickname+'</span>'+
                                             '<i class="fr">'+data_lsit[i].createtime+'</i>'+
                                             '</div>'+
                                             '<p class="comment-font">'+data_lsit[i].content+'</p>'+
                                             '</li>'
                            });
                            setTimeout(function(){
                                $('.comment-list-wrap').html(data_html);
                            },1000);
                        }
                    }
                }
            }
        });


    });
});
































